<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use think\Request;
use think\Session;

class Login extends Base
{
    public function index()
    {
        $data = $this->initData();
        $this->view->engine->layout(false);
        $data['title'] = '登录';
        return view('index', $data);
    }

    public function logout()
    {
        Session::set('userInfo', null);
        $this->redirect('Index/index');
    }

    public function login()
    {
        $username = input('username');
        $password = input('password');
        if (empty($username)) {
            $this->error('账号名或密码不能为空');
        }
        $info  =Admin::get(['username'  => 'root']);
        if (!$info) {
            $this->error('账号密码错误或不存在');
        }
        if ($info->password !== sha1(md5($password))) {
            $this->error('账号密码错误或不存在');
        }
        Session::set('userInfo', [
            'username'  => $username,
            'admin_id'  => $info->id,
            'nickname'  => $info->nickname
        ]);
        $this->success('登录成功', 'Index/index');
    }
}
